﻿/// <reference path="js/jquery-1.6.4.min.js" />
/// <reference path="jquery-1.6.4-vsdoc.js" />

var directionsService = new google.maps.DirectionsService();
var directionsDisplay;

var map, geocoder, marker, ey, my, mouseDown = false;

var places = {
    init: function () {
        this.map.init();
        //this.locations.show();
        this.locations.click();
        this.scroll.init();
        this.locations.search();
        this.locations.directions();
    },
    locations: {
        get: function () {
            var arr = new Array;
            $('.get').find('input').each(function (i) {
                var t = $(this),
                    val = t.val();
                arr[i] = val;
            });
            return arr;
        },
        show: function (base, desc) {
                    //Geocode the location
                    geocoder.geocode({ address: base }, function (response, status) {

                        if (status == google.maps.GeocoderStatus.OK) {
                            //Get the lat and lang
                            var lat = response[0].geometry.location.lat(),
                                lang = response[0].geometry.location.lng();

                            //Set the marker
                            marker = new google.maps.Marker({
                                //icon: img,
                                map: map,
                                title: base,
                                position: new google.maps.LatLng(lat, lang)
                            });

                           map.setCenter(new google.maps.LatLng(lat, lang));
                           map.setZoom(12);

                            google.maps.event.addListener(marker, 'click', function () {
                                places.locations.open(desc, base);
                            });

                            google.maps.event.addListener(marker, 'mouseover', function () {
                                places.locations.open(desc, base);
                            });

                        }
                        else
                        {
                            alert(status);
                        } // end if
                    }); // end geocoder
            
        },
        click: function () {
            $('.twitter').find('.open').live('click', function () {
                var t = $(this), rel = t.attr('rel'), name = t.attr('name');
                places.locations.show(rel, name);
                places.locations.open(name, rel);
            });
        },
        open: function (desc, address) {
            var posts = $('.posts'), arr = new Array;
            arr.push('<div class="post">');
            arr.push(desc + "<br /> <br /> <strong>Address: " + address + "</strong>");
            arr.push('</div>');
            var html = arr.join('');
            posts.html(html).fadeIn();
            //var LatLng = new google.maps.LatLng(38.865291,-77.063448);
            //map.setCenter(LatLng);
        }, // end open
        search: function (input) {
            $('#search').find('#btSearch').live('click', function () {
                var input = $('#txtSearch').val();
                places.locations.show(input);
            });
        }, //end search
        directions: function () {
            $('#search').find('#btDirect').live('click', function () {

                var start = $('#txtStart').val(),
                    end = $('#txtEnd').val();

                var request = {
                    origin:start,
                    destination:end,
                    travelMode: google.maps.TravelMode.DRIVING
                  };

            
                directionsService.route(request, function(results, status) {
                 if (status == google.maps.DirectionsStatus.OK) {
                      directionsDisplay.setDirections(results);
                 }
                 else{
                    alert(status);
                 }
                });
            });
                        
        } //end directions
     }, // end locations
    map: {
        size: function () {
            //Set the size of the map
            var w = $(window).width(),
                h = $(window).height();
            return { width: w, height: h }
        },
        data: {
            zoom: 4,
            center: new google.maps.LatLng(38, -98),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        },
        init: function () {
            //Grab the size of the window in a {width, height} object
            var size = places.map.size();

            //Set the width of height of the #map div accordingly
            $('#map').css({
                width: size.width,
                height: size.height
            });

            //Set the global map variable with the properties set in the data object
            map = new google.maps.Map(document.getElementById('map'), places.map.data),
            
            // Directions display
            directionsDisplay = new google.maps.DirectionsRenderer();
            directionsDisplay.setMap(map);
            directionsDisplay.setPanel(document.getElementById("directionsPanel"));

            //Create the geocoder
            geocoder = new google.maps.Geocoder();

            google.maps.event.addListener(map, 'dragstart', function () {
                $('.posts').hide();
            });


        }, //end map init
    }, // end map
    scroll: {
        mouse: function(e){
            var y = e.pageY; 
            return y;
        },
        check: function(y){
            var all = $('.twitter').height(),
                inside = $('.twitter').find('.inside').height();
            if (y < (all - inside)) {
                y = all - inside;
            } else if (y > 0) {
                y = 0;
            }
            return y;
        },
        update: function(e){
            var y = places.scroll.mouse(e),
                movey = y-my,
                top = ey+movey;
                check = places.scroll.check(top);
            $('.twitter').find('.inside').css({ top: check+'px' });
        },
        init: function(){
            $('.twitter').find('.inside').bind({
                mousedown: function(e){
                    e.preventDefault();
                    mouseDown = true;
                    var mouse = places.scroll.mouse(e);
                        my = mouse;
                    var element = $(this).position();
                        ey = element.top;
                    places.scroll.update(e);
                },
                mousemove: function(e){
                    if (mouseDown)
                        places.scroll.update(e);
                    return false;
                },
                mouseup: function(){
                    if (mouseDown)
                        mouseDown = false;
                    return false;
                },
                mouseleave: function(){
                    if (mouseDown)
                        mouseDown = false;
                    return false;
                }
            });
        }
    }
} // end places

//launch places
$(function () {
    places.init();
});
